package com.powerunion.extjs;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

import com.powerunion.extjs.utils.Convert;


public class BasePara implements Serializable
{	
	//页面传入
	private int start;
	private int limit;
	private String sort="";
	private String dir="";
	private String sqlpath="";
	
	private ArrayList baseValue = null;//单个参数
	
	//复杂参数
	private String paramStr="";
	private ArrayList paramValue = null;
	private ArrayList paramType = null;
	
	//动态查询参数
	private String queryStr="";
	private String queryValue="";//查询值
	
	private boolean paging=false;
	private String recordId="idno";
	private String xmlFileName="";
	private Map treeCfg=null;
	
	//传出参数(需根据传入参数解析)
	private String orderStr = "";
	private String sql = ""; //主sql串
	private String sumsql="";//合计sql
	private String groupSql =""; //分组sql串
	private String type = "";//sql 类型：sql;hql;procedure  默认sql
	private String[][] params = null; 
	
	public void addWhereSql(){
		Convert c = new Convert();
		//add baseParams
		ArrayList pValue = new ArrayList();
		ArrayList pType= new ArrayList();
		
		//替换树的查询sql
		String searchSql = null!=this.treeCfg ? this.treeCfg.get("search_sql")+"" : "";
		if(searchSql!=null && !searchSql.trim().equals("") && 
				(queryValue!=null && !queryValue.trim().equals(""))){ 
			this.sql = searchSql; //树，用查询语句替换
		}
		
		//添加单个参数
		if(null!=baseValue){
			pValue.addAll(baseValue);
			for(int i=0;i<baseValue.size();i++){
				pType.add("String");
			}
		}
		
		//添加复杂参数
		if(paramValue != null){
			int k = this.sql.indexOf("$paramStr");
			int start= (k>=0) ? c.getMarkNum(this.sql.substring(0,k), "?") : pValue.size();
			pValue.addAll(start,paramValue);
			pType.addAll(start,paramType);
		}else{
			paramStr = "";
		}
		this.appendSql(paramStr,"$paramStr");
		
		//添加动态查询参数
		if(queryValue!=null && !queryValue.trim().equals("")){
			int k = this.sql.indexOf("$queryStr");
			int start= (k>=0) ? c.getMarkNum(this.sql.substring(0,k), "?") : pValue.size();
			int qmarknum = c.getMarkNum(queryStr, "?");
			for(int j=0;j<qmarknum;j++){
				pValue.add(start,queryValue);
				pType.add(start,"String");
				start++;
			}
		}else{
			queryStr = "";
		}
		this.appendSql(queryStr,"$queryStr");
		
		//赋值
		if(null!=pValue && pValue.size()>0){
			String[] v = new String[pValue.size()];
			String[] t = new String[pType.size()];
			pValue.toArray(v);
			pType.toArray(t);
			System.out.println(pValue);
			this.params = new String[][]{v,t};
		}
	}
	
	public void appendSql(String str,String regex){
		if(null!=str && !"".equals(str.trim())){
			boolean isConnctBy = sql.indexOf("connect")>=0;
			if(!isConnctBy){
				if(sql.indexOf("where")<0){
					str = " where (" + str+")";
				}
				else{
					str = " and (" + str + ")";
				}
			}
		}
		if(null!=regex && this.sql.indexOf(regex)>=0){
			this.sql = this.sql.replaceFirst("\\"+regex, str);
		}else{
			this.sql = this.sql + str ;		
		}
				
	}
	
	public void addGroupSql(){
		if(null!=groupSql){
			this.sql = this.sql + groupSql ;	
		}
	}
	
	public void addOrderSql(){
		this.orderStr = this.sort == null || this.sort.equals("") ? "" : " order by " + this.sort + " " + this.dir;
		String regex = "$orderStr";
		if(this.sql.indexOf(regex)>=0){
			this.sql = this.sql.replaceFirst("\\"+regex, this.orderStr);
		}else{
			this.sql = sql + this.orderStr;
		}		
	}	
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getDir() {
		return dir;
	}
	public void setDir(String dir) {
		this.dir = dir;
	}
	public String getSqlpath() {
		return sqlpath;
	}
	public void setSqlpath(String sqlpath) {
		this.sqlpath = sqlpath;
	}
	public String getParamStr() {
		return paramStr;
	}
	public void setParamStr(String paramStr) {
		this.paramStr = paramStr;
	}
	public String getOrderStr() {
		return orderStr;
	}
	public void setOrderStr(String orderStr) {
		this.orderStr = orderStr;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public boolean isPaging() {
		return paging;
	}
	public void setPaging(boolean paging) {
		this.paging = paging;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public String getQueryStr() {
		return queryStr;
	}

	public void setQueryStr(String queryStr) {
		this.queryStr = queryStr;
	}

	public String[][] getParams() {
		return params;
	}

	public void setParams(String[][] params) {
		this.params = params;
	}

	public String getGroupSql() {
		return groupSql;
	}

	public void setGroupSql(String groupSql) {
		this.groupSql = groupSql;
	}

	public String getXmlFileName() {
		return xmlFileName;
	}

	public void setXmlFileName(String xmlFileName) {
		this.xmlFileName = xmlFileName;
	}

	public String getRecordId() {
		return recordId;
	}

	public void setRecordId(String recordId) {
		this.recordId = recordId;
	}

	public Map getTreeCfg() {
		return treeCfg;
	}

	public void setTreeCfg(Map treeCfg) {
		this.treeCfg = treeCfg;
	}

	public ArrayList getBaseValue() {
		return baseValue;
	}

	public void setBaseValue(ArrayList baseValue) {
		this.baseValue = baseValue;
	}

	public ArrayList getParamValue() {
		return paramValue;
	}

	public void setParamValue(ArrayList paramValue) {
		this.paramValue = paramValue;
	}

	public ArrayList getParamType() {
		return paramType;
	}

	public void setParamType(ArrayList paramType) {
		this.paramType = paramType;
	}

	public String getQueryValue() {
		return queryValue;
	}

	public void setQueryValue(String queryValue) {
		this.queryValue = queryValue;
	}

	public String getSumsql() {
		return sumsql;
	}

	public void setSumsql(String sumsql) {
		this.sumsql = sumsql;
	}
	
	
}